package cn.itsource.utils;

import com.alibaba.fastjson.JSONObject;

public class DistanceUtil {
    private static final String ak = "OZCyMqbsc1ntn9ahGHyKWKvxGZ3gcpvS";
    /**
     * 地址转经纬坐标点
     */
    private static final String API_ADDRESS_TO_POINT = "http://api.map.baidu.com/geocoding/v3/?address=ADDRESS&output=json&ak=MYAK";
    /**
     * 计算2个point之间的驾车距离
     */
    private static final String API_CAL_DISTANCE_TWO_POINT = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=POINT1&destinations=POINT2&ak=" + ak;


    /**
     * 根据传入的2个地址，计算2个地址之间的距离
     *
     * @param address1
     * @param address
     * @return
     */
    public static Double calTwoAddressOfDistance(String address1, String address) {
        Point point1 = calAddressToPoint(address1);
        Point point2 = calAddressToPoint(address);
        if (point1 == null || point2 == null) {
            return null;
        }
        return calJuliByTwoPoint(point1, point2);
    }

    /**
     * 根据地址计算对应的经纬坐标
     *
     * @param address
     */
    private static Point calAddressToPoint(String address) {
        //下面的地址，是添加了必填参数之后的api地址
        String newApi = API_ADDRESS_TO_POINT.replace("ADDRESS", address).replace("MYAK", ak);

        //在后台，模拟发送一个http协议请求
        HttpClient httpClient = new HttpClient(newApi);
        httpClient.get();
        String content = httpClient.getContent();
//        System.out.println(content);
        //{"status":0,"result":{"location":{"lng":116.40396298757887,"lat":39.91511908708907},"precise":0,"confidence":50,"comprehension":100,"level":"旅游景点"}}
        JSONObject jsonObject = JSONObject.parseObject(content);//将查询的json字符串转为json对象
        if (jsonObject.getString("status") == "0") {//返回的状态不是0，说明地址解析有误
            JSONObject pointObject = jsonObject.getJSONObject("result").getJSONObject("location");
            return new Point(pointObject.getDouble("lng"), pointObject.getDouble("lat"));
        } else {
            return null;
        }

    }

    /**
     * 计算2个经纬坐标点之间的 驾车距离
     *
     * @param p1
     * @param p2
     */
    private static double calJuliByTwoPoint(Point p1, Point p2) {

        //在后台，模拟发送一个http协议请求
        HttpClient httpClient = new HttpClient(API_CAL_DISTANCE_TWO_POINT.replace("POINT1", p1.parsePoint()).replace("POINT2", p2.parsePoint()));
        httpClient.get();
        String content = httpClient.getContent();
//        System.out.println(content);
        //{"status":0,"result":[{"distance":{"text":"4165.8公里","value":4165846},"duration":{"text":"43.4小时","value":156216}}],"message":"成功"}
        JSONObject jsonObject = JSONObject.parseObject(content);
        double distance = jsonObject.getJSONArray("result").getJSONObject(0).getJSONObject("distance").getDouble("value");

        return distance;
    }


    public static void main(String[] args) {
//        Point point = calAddressToPoint("湖北省武汉市长城科技园");
//        System.out.println(point);
//        calJuliByTwoPoint(new Point(112.00, 22.00), new Point(88.12, 44.23));

        //测试2个地址之间距离
        double distance = calTwoAddressOfDistance("湖北省武汉市江夏区长城科技园", "湖北省仙桃市汽车客运站");
        System.out.println(distance);
    }
}